﻿using System;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using System.Drawing.Imaging;
using AvatarHelper;
using Avatar.Model;
using Avatar.MembershipService;
using Avatar.Common;

/// <summary>
/// 这个页面类主要实现前端注册
/// </summary>
public partial class Company_Map : System.Web.UI.Page
{
    string _op = "1";

    protected void Page_Load(object sender, EventArgs e)
    {
        var _x = Request.QueryString["x"] != null ? Request.QueryString["x"].ToString() : "115.475149";
        var _y = Request.QueryString["y"] != null ? string.IsNullOrEmpty(Request.QueryString["y"].ToString()) ? "38.869652" : Request.QueryString["y"].ToString() : "38.869652";
        _op = Request.QueryString["op"] != null ? string.IsNullOrEmpty(Request.QueryString["op"].ToString()) ? "1" : Request.QueryString["op"].ToString() : "1";

        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _user = LoginManager.GetCurrentUser();
                if (_user != null)
                {
                    var _companyMap = _adc.CompanyMap.FirstOrDefault(m => m.User == _user.UserName);
                    if (_op == "1")
                    {
                        if (_companyMap != null)
                        {
                            _x = _companyMap.X;
                            _y = _companyMap.Y;
                        }
                    }
                    else if (_op == "2")
                    {
                        if (_companyMap == null)
                        {
                            _companyMap = new CompanyMap();
                            _companyMap.User = _user.UserName;
                            _companyMap.X = _x;
                            _companyMap.Y = _y;

                            _adc.CompanyMap.InsertOnSubmit(_companyMap);
                        }
                        else
                        {
                            _companyMap.X = _x;
                            _companyMap.Y = _y;
                        }
                        _adc.SubmitChanges();
                        Helper.ShowMassage("地图标注信息保存成功！");
                    }
                }

                var _script = @"<script language='JavaScript'>var mapOptions = new MMapOptions();mapOptions.zoom = 15;mapOptions.center = new MLngLat(" + _x + ", " + _y + ");var mapObj = new MMap('mapObj', mapOptions);function getCenterByLatLng(event){var center = mapObj.getCenter();self.location = './company_map.aspx?x=' + center.lngX + '&y=' + center.latY + '&op=2';}</script>";

                lbl_Script.Text = _script;
            }
        }
    }
}
